package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 注册用户资源控制器
 */
@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService travelCommendService;

    @PostMapping
    public JSONResult save(Travel travel) {
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    @GetMapping("/{id}")
    public Travel getById(@PathVariable Long id) {

        return travelService.selectByPrimaryKey(id);
    }

    @PutMapping("{id}")
    public JSONResult update(Travel travel) {
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    /**
     * 获取全部游记
     * @param queryObject
     * @return
     */
    @GetMapping
    public PageInfo queryTravel(TravelQueryObject queryObject) {
        queryObject.setOrderBy("t.releaseTime desc");
        queryObject.setState(Travel.STATE_RELEASE);
        PageInfo pageInfo= travelService.query(queryObject);
        return pageInfo;
    }
    @GetMapping("/commends")
    public PageInfo getCommendTravels(TravelCommendQueryObject queryObject) {
        //推荐时间倒序
        queryObject.setOrderBy("tc.schedule desc");
        //在今天之前
        queryObject.setBeforeToday(true);
        return travelCommendService.queryForAppList(queryObject);
    }

}
